package com.black.app.service.impl.exercise;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.exercise.AppExerciseGroup;
import com.black.app.domain.req.exercise.add.AppExerciseActionAddReq;
import com.black.app.domain.req.exercise.add.AppExerciseGroupAddReq;
import com.black.app.domain.req.exercise.edit.AppExerciseGroupEditReq;
import com.black.app.service.exercise.AppExerciseActionService;
import com.black.app.service.exercise.AppExerciseGroupService;
import com.black.app.mapper.exercise.AppExerciseGroupMapper;
import com.black.common.exception.ServiceException;
import com.black.common.utils.MessageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author L
 * @description 针对表【app_exercise_group】的数据库操作Service实现
 * @createDate 2024-10-23 16:57:08
 */
@Service
public class AppExerciseGroupServiceImpl extends ServiceImpl<AppExerciseGroupMapper, AppExerciseGroup>
        implements AppExerciseGroupService {

    @Autowired
    private AppExerciseActionService actionService;

    /**
     * 根据系列id查询对应的锻炼动作组
     *
     * @param seriesId
     * @return
     */
    @Override
    public List<AppExerciseGroup> findBySId(Long seriesId) {
        return this.baseMapper.findBySId(seriesId);
    }

    /**
     * 添加动作组
     *
     * @param req
     * @return
     */
    @Override
    public boolean add(AppExerciseGroupAddReq req) {

        // 获取当前系列分组的最大值
        Integer maxGroupNum = this.baseMapper.selectOne(new LambdaQueryWrapper<AppExerciseGroup>()
                .eq(AppExerciseGroup::getSId, req.getSid())
                .orderByDesc(AppExerciseGroup::getGroupNum).last("limit 1")).getGroupNum();

        AppExerciseGroup groups = new AppExerciseGroup();
        groups.setGroupNum(maxGroupNum + 1);
        groups.setRepeats(req.getRepeats());
        groups.setSId(req.getSid());

        return this.save(groups);
    }

    /**
     * 修改动作组
     *
     * @param req
     * @return
     */
    @Override
    public boolean edit(AppExerciseGroupEditReq req) {
        AppExerciseGroup group = this.baseMapper.findBySIdAndGroup(req.getSid(), req.getGroupNum());
        if (Objects.nonNull(group)) {
            group.setRepeats(req.getRepeats());
            return this.updateById(group);
        }
        return false;
    }

    @Override
    public AppExerciseGroup findById(Long groupId) {
        return this.baseMapper.findByGId(groupId);
    }

    /**
     * 根据系列id和分组id查询动作组信息
     *
     * @param sid
     * @param gid
     * @return
     */
    @Override
    public AppExerciseGroup findBySidAndGid(Long sid, Long gid) {
        AppExerciseGroup group = this.baseMapper.findBySidAndGid(sid, gid);
        return group;
    }

    /**
     * 删除动作组
     * @param groupId
     * @return
     */
    @Override
    public boolean deleteById(Long groupId) {
        AppExerciseGroup group = this.findById(groupId);
        if (Objects.nonNull(group)) {
            // 判断当前动作组下是否有动作
            if (this.actionService.findByGId(groupId).size() > 0) {
                throw new ServiceException(MessageUtils.message("exercisegroup.not.empty"));
            }
        }
        return this.removeById(groupId);
    }
}




